using System;
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataListPage : System.Web.UI.Page
{
    int pageSize = 9;
    ControlHelper controlHelper;
    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString =
            WebConfigurationManager.ConnectionStrings[
            "RCVDB2000"].ConnectionString;

        controlHelper = new  
        ControlHelper(connectionString);
        controlHelper.CommandText = 
        "select Email,FirstName+ ' '" +
        "+LastName as FullName, Address" +
        ",City from tblJSAccounts ";
        controlHelper.CommandType = 
        CommandType.Text;
        
        controlHelper.InitProperties();    
                    
        if (!IsPostBack)
        {
            lblPage.Text = "1";
            txtPage.Text = "1";
            controlHelper.FillDataTableToDataList(
            this.DataList1,0,pageSize);
            controlHelper.CommandText = 
                "select count(*)  from " +
                "tblJSAccounts ";
            controlHelper.CommandType = CommandType.Text;
            controlHelper.InitProperties();
            int rows = controlHelper.GetTotalRecords();
            lblPages.Text = (rows / pageSize 
            + ((rows % pageSize) > 0 ? 1 : 0)).ToString();
            rvPage.MaximumValue = lblPages.Text;
            if (rows <= pageSize)
            {
                linkPrevious.Visible = false;
                linkFirst.Visible = false;
                linkLast.Visible = false;
                linkNext.Visible = false;
            } 
            else
            {
                linkPrevious.Enabled = false;
                linkFirst.Enabled = false;
            }
        }
    }


    protected void linkFirst_Click(object sender, EventArgs e)
    {
        
        controlHelper.FillDataTableToDataList(this.DataList1, 0, pageSize);
        lblPage.Text = "1";
        txtPage.Text = "1";
        linkPrevious.Enabled = false;
        linkFirst.Enabled = false;
        linkNext.Enabled = true;
        linkLast.Enabled = true;
    }
    protected void linkPrevious_Click(object sender, EventArgs e)
    {
        linkNext.Enabled = true;
        linkLast.Enabled = true;
        int currentPage = 
        Convert.ToInt32(lblPage.Text);
        if (currentPage > 1) currentPage--;
         
        controlHelper.FillDataTableToDataList(
        this.DataList1,
    (currentPage - 1) * pageSize, pageSize);
        lblPage.Text = currentPage.ToString();
        txtPage.Text = lblPage.Text;
        if (currentPage == 1)
        {
            linkPrevious.Enabled = false;
            linkFirst.Enabled = false;
        }
        else
        {
            linkFirst.Enabled = true;
            linkPrevious.Enabled = true;
        }
    }

    protected void linkNext_Click(object sender, EventArgs e)
    {
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        int currentPage = 
        Convert.ToInt32(lblPage.Text);
        if (currentPage > 0) currentPage++;
         
        controlHelper.FillDataTableToDataList(
        this.DataList1,
    (currentPage - 1) * pageSize, pageSize);
        lblPage.Text = currentPage.ToString();
        txtPage.Text = lblPage.Text;
        if (currentPage == 
        Convert.ToInt32(lblPages.Text))
        {
            linkLast.Enabled = false;
            linkNext.Enabled = false;
        }
        else
        {
            linkLast.Enabled = true;
            linkNext.Enabled = true;
        }
    }


    protected void linkLast_Click(object sender, EventArgs e)
    {
        linkNext.Enabled = false;
        linkLast.Enabled = false;
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
         
        controlHelper.FillDataTableToDataList(
        this.DataList1, (Convert.ToInt32(lblPages.Text) 
        - 1) * pageSize, pageSize);
        lblPage.Text = lblPages.Text;
        txtPage.Text = lblPage.Text;
    }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        int currentPage = Convert.ToInt32(txtPage.Text);

        
        controlHelper.FillDataTableToDataList(this.DataList1,
        (currentPage - 1) * pageSize, pageSize);
        lblPage.Text = currentPage.ToString();
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        linkLast.Enabled = true;
        linkNext.Enabled = true;
        if (currentPage == 
        Convert.ToInt32(lblPages.Text))
        {
            linkLast.Enabled = false;
            linkNext.Enabled = false;

        }
        if (currentPage == 1)
        {

            linkFirst.Enabled = false;
            linkPrevious.Enabled = false;
        }
    }
     
}
